package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 里
 */
@RestController
@RequestMapping("/admin/product")
public class ManageController {

    @Autowired
    private ManageService manageService;

    // 获取一级分类数据
    @GetMapping("/getCategory1")
    public Result getCategory1(){
        List<BaseCategory1> baseCategory1List = manageService.getAllBaseCategory1();
        return Result.ok(baseCategory1List);
    }

    // 获取二级分类数据
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable(value = "category1Id") Long category1Id){
        List<BaseCategory2> baseCategory2List = manageService.getAllBaseCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }

    // 获取三级分类数据
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable(value = "category2Id") Long category2Id){
        List<BaseCategory3> baseCategory3List = manageService.getAllBaseCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }

    //获取平台属性集合数据
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getAttrInfoList(@PathVariable(value = "category1Id") Long category1Id,
                                  @PathVariable(value = "category2Id") Long category2Id,
                                  @PathVariable(value = "category3Id") Long category3Id){
       List<BaseAttrInfo> baseAttrInfoList =  manageService.getAttrInfoList(category1Id,category2Id,category3Id);
        return Result.ok(baseAttrInfoList);
    }

    // 保存平台属性
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    // 根据属性Id来获取属性值集合
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList (Long attrId){
     // List<BaseAttrValue> baseAttrValueList =   manageService.getAttrValueList(attrId);
        //优化 只有属性存在，再去查询属性值
      BaseAttrInfo baseAttrInfo =   manageService.getAttrInfo(attrId);
      return Result.ok(baseAttrInfo.getAttrValueList());
    }


}
